package cn.wolfcode.web.controller;


import cn.wolfcode.enums.BusinessReportEnum;
import cn.wolfcode.enums.ConsumptionStatusEnum;
import cn.wolfcode.qo.BusinessReportQueryObject;
import cn.wolfcode.service.IBusinessReportService;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.util.RequiredPermission;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/businessReport")
public class BusinessReportController {

    @Autowired
    private IBusinessReportService businessReportService;
    @Autowired
    private IBusinessService businessService;


    @RequiredPermission(name = "门店收入管理页面", expression = "businessReport:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") BusinessReportQueryObject qo){
        model.addAttribute("pageInfo", businessReportService.selectReport(qo));
        model.addAttribute("businesses", businessService.listAll());
        model.addAttribute("consumptionStatusEnums", ConsumptionStatusEnum.values());
        model.addAttribute("businessReportEnums", BusinessReportEnum.values());

        return "businessReport/list";
    }

    @RequiredPermission(name = "门店页面", expression = "businessReport:listBar")
    @RequestMapping("/listBar")
    public String listBar(Model model, @ModelAttribute("qo") BusinessReportQueryObject qo){
        ArrayList<Object> groupTypeList = new ArrayList<>();
        ArrayList<Object> numberList = new ArrayList<>();
        ArrayList<Object> payAmountList = new ArrayList<>();
        ArrayList<Object> totalAmountList = new ArrayList<>();
        ArrayList<Object> discountAmountList = new ArrayList<>();

        qo.setPageSize(0);
        List<HashMap> list = businessReportService.selectReport(qo).getList();
        list.forEach(hashMap -> {
            groupTypeList.add(hashMap.get("groupType"));
            numberList.add(hashMap.get("number"));
            payAmountList.add(hashMap.get("payAmount"));
            totalAmountList.add(hashMap.get("totalAmount"));
            discountAmountList.add(hashMap.get("discountAmount"));
        });

        model.addAttribute("groupTypeList", JSON.toJSONString(groupTypeList));
        model.addAttribute("numberList", JSON.toJSONString(numberList));
        model.addAttribute("payAmountList", JSON.toJSONString(payAmountList));
        model.addAttribute("totalAmountList", JSON.toJSONString(totalAmountList));
        model.addAttribute("discountAmountList", JSON.toJSONString(discountAmountList));
        return "listBar.ftl";
    }



}
